package com.xzj.admin.analysis.base.custom.ty;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.common.handle.CommonAbstractAnalysisHandle;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: xzj-admin
 * @ClassName: ZyyAnalysisHandle
 * @description: 天翼自研云
 * @author: XiaZunJun
 * @create: 2024-11-12 22:17
 */
@Slf4j
public class YcYcAnalysisHandle extends CommonAbstractAnalysisHandle {

    static {
        registerJudgeSubFun("physicalHost", YcYcAnalysisHandle::physicalHostSubResource);
        registerJudgeSubFun("virtualHost", TyCommonHandle::virtualHostSubResource);
        registerJudgeSubFun("virtualComputer", TyCommonHandle::virtualHostSubResource);
        registerJudgeSubFun("virtualVolume", TyCommonHandle::virtualVolumeSubResource);
        registerJudgeSubFun("virtualStoragePool", TyCommonHandle::virtualStoragePoolSubResource);
        registerJudgeSubFun("afterSales", TyCommonHandle::physicalHostAfterSalesSub);
        registerJudgeSubFun("poolList", TyCommonHandle::physicalHostPoolListSub);
        registerJudgeSubFun("dcId", YcYcAnalysisHandle::physicalHostDcIdSub);
        registerJudgeSubFun("rackId", YcYcAnalysisHandle::physicalHostRackIdSub);
        registerJudgeSubFun("roomId", YcYcAnalysisHandle::physicalHostRoomIdSub);
        registerJudgeSubFun("buildingId", YcYcAnalysisHandle::physicalHostBuildingIdSub);
    }

    public YcYcAnalysisHandle(String cloudId,String cloudType) {
        super(cloudId, cloudType,new TyyThemeSpecialAnalysisHandle(cloudId, cloudType));
    }


    private static void physicalHostSubResource(JSONObject row, String parentId, String fieldName,
                                                JSONArray subThemeRows) {
        if(isJudgeSubFun(fieldName)){
            getJudgeSubFun(fieldName).accept(row, parentId, fieldName,subThemeRows);
        }
    }

    private static void idSubCommon(JSONObject row, String fieldName,
                                    JSONArray subThemeRows,String type) {
        String id = row.getString(fieldName);
        if (StrUtil.isNotEmpty(id)) {
            try {
                JSONObject buildingObj = new JSONObject();
                buildingObj.put("id", id);
                buildingObj.put("type", type);
                subThemeRows.add(buildingObj);
            } catch (Exception e) {
                log.error("添加子资源失败", e);
            }
        }
    }


    /**
     * 物理主机 机楼子资源
     * @author XiaZunJun
     * @date 2024/11/12 下午10:35
     * @param row 当前解析对象
     * @param parentId 父资源Id
     * @param fieldName 组装子资源的对象字段
     * @param subThemeRows 子资源集合
     *
     */
    private static void physicalHostBuildingIdSub(JSONObject row, String parentId, String fieldName,
                                                  JSONArray subThemeRows) {
        idSubCommon(row, fieldName, subThemeRows,"BUILDING");
    }


    private static void physicalHostRoomIdSub(JSONObject row, String parentId, String fieldName,
                                              JSONArray subThemeRows) {
        idSubCommon(row, fieldName, subThemeRows,"ROOM");
    }

    private static void physicalHostRackIdSub(JSONObject row, String parentId, String fieldName,
                                              JSONArray subThemeRows) {
        idSubCommon(row, fieldName, subThemeRows,"RACK");
    }

    private static void physicalHostDcIdSub(JSONObject row, String parentId, String fieldName,
                                            JSONArray subThemeRows) {
        idSubCommon(row, fieldName, subThemeRows,"POOL");
    }

}
